package main;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.ByteArrayBuffer;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.Log;

public class W {
	static String FILES_DIR;

	static void init(Context context) {
		FILES_DIR = context.getFilesDir().getAbsolutePath()
				+ File.separatorChar;

		copyAssets(context);
	}

	static String getRawResourceLink(Context context, String resourceFilename) {
		return context.getFilesDir() + "raw/" + resourceFilename;
	}

	static String getFileAddress(String filename) {
		return FILES_DIR + filename;
	}

	static String getFileURI(String filename) {
		return "file://" + FILES_DIR + filename;
	}

	// gets a web page from url return context as string
	static String getPage(String urlString) {
		HttpClient client = new DefaultHttpClient();
		HttpGet request = new HttpGet(urlString);
		HttpResponse response;
		String html = "";
		StringBuilder str = new StringBuilder();
		try {
			response = client.execute(request);

			InputStream in = response.getEntity().getContent();
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(in));
			String line = null;
			while ((line = reader.readLine()) != null) {
				str.append(line);
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		html = str.toString();

		return html;
	}

	// copies file from assets dir to applications local dir
	static boolean copyFileFromAssets(Context context, String fileName) {
		boolean status = false;
		try {
			FileOutputStream out = context.openFileOutput(fileName,
					Context.MODE_PRIVATE);
			InputStream in = context.getAssets().open(fileName);
			// Transfer bytes from the input file to the output file
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			// Close the streams
			out.close();
			in.close();
			status = true;
		} catch (Exception e) {
			System.out.println("Exception in copyFile:: " + e.getMessage());
			status = false;
		}
		System.out.println("copyFile Status:: " + status);
		return status;
	}

	private static boolean copyAssets(Context context) {
		AssetManager assetManager = context.getAssets();
		String[] files = null;
		try {
			files = assetManager.list("");
		} catch (IOException e) {
			Log.e("tag", e.getMessage());
		}

		boolean result = false;
		for (String filename : files) {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = assetManager.open(filename);
				out = new FileOutputStream(FILES_DIR + filename);
				copyFile(in, out);
				in.close();
				in = null;
				out.flush();
				out.close();
				out = null;
			} catch (Exception e) {
				Log.e("tag", e.getMessage());
				result &= false;
			}
		}
		return result;
	}

	private static void copyFile(InputStream in, OutputStream out)
			throws IOException {
		byte[] buffer = new byte[1024];
		int read;
		while ((read = in.read(buffer)) != -1) {
			out.write(buffer, 0, read);
		}
	}

	static String readTextFile(String filename) {
		int BUFFER_SIZE = 1024;
		ByteArrayBuffer bab = new ByteArrayBuffer(BUFFER_SIZE);
		try {
			InputStream is = new FileInputStream(new File(filename));
			BufferedInputStream bis = new BufferedInputStream(is);
			byte[] buffer = new byte[BUFFER_SIZE];
			while (bis.read(buffer, 0, BUFFER_SIZE) != -1) {
				bab.append(buffer, 0, BUFFER_SIZE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}

		return new String(bab.toByteArray());

	}

	static String readTextResource(Resources resources, int resId) {
		InputStream is = resources.openRawResource(resId);
		BufferedInputStream bis = new BufferedInputStream(is);
		ByteArrayBuffer baf = new ByteArrayBuffer(50);
		int current = 0;
		try {
			while ((current = bis.read()) != -1) {
				baf.append((byte) current);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		byte[] myData = baf.toByteArray();
		return new String(myData);

	}

	static Drawable getDrawableFromWebURL(String urlString) {
		try {
			InputStream is = (InputStream) new URL(urlString).getContent();
			Drawable d = Drawable.createFromStream(is, "src name");
			return d;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	static boolean saveFileFromUrl(String sourceUrlString, File destinationFile) {
		InputStream is = null;
		FileOutputStream fos = null;
		try {
			is = (InputStream) new URL(sourceUrlString).getContent();
			fos = new FileOutputStream(destinationFile);
			byte[] buffer = new byte[1024];
			int byteCount;
			while ((byteCount = is.read(buffer)) != -1) {
				fos.write(buffer, 0, byteCount);
			}
			fos.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}
}
